﻿using System;
using WebPattern.Core.Logger;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    #region Properties
    static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(System.Web.UI.MasterPage));
    static readonly CustomLog4net _customLogger = new CustomLog4net(_logger);
    /// <summary>
    /// Loger
    /// </summary>
    public log4net.ILog Logger
    {
        get { return _logger; }
    }
    /// <summary>
    /// Custom Logger
    /// </summary>
    public static CustomLog4net CLogger
    {
        get { return _customLogger; }
    }
    /// <summary>
    /// Culture name 
    /// </summary>
    public string CurrentCultureName
    {
        get { if (Session["__currentLanguage"] != null) return Session["__currentLanguage"].ToString(); else return string.Empty; }
    }
    /// <summary>
    /// TRUE -> When site language was changed; FALSE - else;
    /// </summary>
    public bool IsLanguageChanging
    {
        get
        {
            if (Session["__isLanguageChanging"] != null)
            {
                try
                {
                    return Convert.ToBoolean(Session["__isLanguageChanging"]);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("File :[{0}] -- Triger [{1}] -- Line [{2}] -- Message [{3}]"
                        , "BasePage.cs"
                        , "IsLanguageChanging"
                        , "36"
                        , "Session[\"__isLanguageChanging\"] is not in corect format -- " + ex.Message);
                    return false;
                }
            }
            else
                return false;
        }
    }
    #endregion

    #region Page life cycle Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        #region Nhibernate tests
        //TestRepository tr = new TestRepository();
        //Test t = tr.GetById(1);
        #endregion NHibernate test

        #region Error logging tests
        //Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception());
        //CLogger.Audit(Operation.Add, "OK");
        //Logger.Error("fake error", new Exception());
        #endregion

        if (IsLanguageChanging)
        {
            lbl.Text += CurrentCultureName; // + " -- " + t.Name + " -- " + t.Description;
            ctr.InitControlsLanguage();
        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {

    }
    #endregion

    #region Events Methods
    protected void rblLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    #endregion

    #region Resources
    protected string GetLocalResource(string resourceKey)
    {
        try
        {
            return GetLocalResourceObject(resourceKey).ToString();
        }
        catch (Exception ex)
        {
            Logger.Error(string.Format("Resources: Value for resource key: '{0}' not found", resourceKey, ex.Message), ex);
            return string.Format("Value for resource key: '{0}' not found", resourceKey, ex.Message);
        }
    }
    #endregion
}
